FROM php:8.1-fpm-alpine3.19

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

#环境基础软件
RUN apk update && apk add --no-cache nginx supervisor tzdata bash && rm -rf /var/cache/apk/*

ARG TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

#composer
RUN curl -s http://getcomposer.org/installer | php &&  mv composer.phar /usr/local/bin/composer && composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

#安装扩展
RUN apk add --no-cache   --virtual .build-deps $PHPIZE_DEPS \
    #没有依赖问题直接安装的扩展
    && docker-php-ext-install pdo_mysql bcmath opcache \
    #zip
    && apk add --no-cache libzip-dev &&  docker-php-ext-install zip \
    #gd
    && apk add --no-cache zlib-dev libpng-dev freetype-dev libjpeg-turbo-dev  && docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install gd \
    #pecl 安装的扩展
    && pecl channel-update pecl.php.net \
    # redis
    && pecl install redis  && echo extension=redis.so >> /usr/local/etc/php/conf.d/docker-php-ext-redis.ini \
    # 清理
    && apk del --purge .build-deps &&  rm -rf /var/cache/apk/* && rm -rf /tmp/pear ~/.pearrc

#将进程加入到supervisor中
RUN set -eux; \
    mkdir -p /etc/supervisord /var/run/supervisord /var/log/supervisord; \
    { \
        echo '[supervisord]'; \
        echo 'logfile=/var/log/supervisord/supervisord.log'; \
        echo 'pidfile = /var/run/supervisord/supervisord.pid'; \
        echo 'nodaemon=true'; \
        echo 'user=root'; \
        echo '[include]'; \
        echo 'files = /etc/supervisord/*.conf'; \
    } | tee /etc/supervisord.conf; \
    { \
        echo '[program:nginx]'; \
        echo 'command=/usr/sbin/nginx -g "daemon off;"' ; \
        echo 'autorestart=true'; \
        echo 'autostart=true'; \
    } | tee /etc/supervisord/nginx.conf; \
    { \
        echo '[program:php-fpm]'; \
        echo 'command=/usr/local/sbin/php-fpm -F'; \
        echo 'autorestart=true'; \
        echo 'autostart=true'; \
    } | tee /etc/supervisord/php-fpm.conf;


RUN set -eux; \
    { \
        echo 'server {'; \
        echo '  listen 80 default_server;'; \
        echo '  server_name _;'; \
        echo '  root /var/www/public;'; \
        echo '  index index.php index.html index.htm;'; \
        echo '  autoindex off;'; \
        echo '  autoindex_exact_size off;'; \
        echo '  autoindex_localtime on;'; \
        echo '  location / {'; \
        echo '      try_files $uri $uri/ /index.php?$query_string;'; \
        echo '  }'; \
        echo '  location ~ \.php$ {'; \
        echo '      try_files $uri /index.php =404;'; \
        echo '      fastcgi_pass 127.0.0.1:9000;'; \
        echo '      fastcgi_index index.php;'; \
        echo '      fastcgi_buffers 16 16k;'; \
        echo '      fastcgi_buffer_size 32k;'; \
        echo '      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'; \
        echo '      include fastcgi_params;'; \
        echo '  }'; \
        echo '}'; \
    } | tee /etc/nginx/http.d/default.conf;

RUN mkdir -p /var/www/public && echo "<?php phpinfo();" >>  /var/www/public/index.php

WORKDIR /var/www

RUN composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/composer/

ENTRYPOINT ["/usr/bin/supervisord","-c","/etc/supervisord.conf"]

EXPOSE 80
